/**
 *
 */
package com.wangyadong.hobby.common.utils;


/**
 * 返回值
 * <p>
 * 这个类包含了3个静态方法 <br>
 * 1.{@link #success(Object need)}<br>
 * 2.{@link #success()}<br>
 * 3.{@link #fail(String errorMsg)}<br>
 * <p>
 *
 * @author wangyadong
 * @since 28.06.2018
 **/
public class HobbyResult {
    /**
     * 消息码，成功200，失败1001400
     **/
    private int code;
    /**
     * 错误消息，"操作成功"或者"操作失败"
     **/
    private String message;
    /**
     * 结果集，任意值
     **/
    private Object data;
    /**
     * 详细的信息，它与message不一样，message描述的是成功还是失败，并不应该描述具体的错误信息，该字段details存在是描述具体的错误信息，
     * 它的读者是前端开发人员，前端不应该直接提示message或者details，前端应该根据code来提示用户
     **/
    private String details;

    private static final String SUCCESS = "操作成功";
    private static final String FAIL = "操作失败";

    /**
     * 默认的构造方法，不对外开放
     */
    private HobbyResult() {
    }

    /**
     * 操作成功，并且有附带的数据时，需要调用该方法
     * <p>
     *
     * @param need 想要的结果
     **/
    public static HobbyResult success(Object need) {
        HobbyResult result = new HobbyResult();
        result.data = need;
        doSuccess(result);
        return result;
    }

    /**
     * 操作成功，没有附带的数据时，需要调用该方法，当然，调用success(null)也是可以的
     * <p>
     **/
    public static HobbyResult success() {
        HobbyResult result = new HobbyResult();
        doSuccess(result);
        return result;
    }

    private static void doSuccess(HobbyResult result) {
        result.code = 200;
        result.message = SUCCESS;
        result.details = SUCCESS;
    }

    /**
     * 操作失败时需要调用该方法
     * <p>
     *
     * @param 错误码
     * @param 详细的错误信息
     **/
    public static HobbyResult fail(String errorMsg) {
        HobbyResult result = new HobbyResult();
        result.code = 1001400;
        result.message = FAIL;
        result.details = errorMsg;
        return result;
    }


    // ====================以下get/set
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

}
